package com.spzx.product.controller;

import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.log.enums.BusinessType;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.Brand;
import com.spzx.product.service.IBrandService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 分类品牌 前端控制器
 * </p>
 *
 * @author biu
 * @since 2025-03-13
 */

@Tag(name = "品牌管理")
@RestController
@RequestMapping("/brand")
public class BrandController extends BaseController {
    @Autowired
    private IBrandService brandService;

    /**
     * 分类品牌列表
     */
    @Log(title = "品牌管理", businessType = BusinessType.QUERY)
    @RequiresPermissions("product:brand:list")
 //   @RequiresRoles("admin")    //角色
    @Operation(summary = "品牌列表")
    @GetMapping("/list")
    public TableDataInfo list(@Parameter(name = "name" ,description = "品牌名称") String name)
    {
        //分页
        startPage();

        //查询
        List<Brand> list = brandService.selectBrandList(name);
        return getDataTable(list);
    }

    //    @RequiresRoles("productAdmin")
//    @RequiresLogin
    @RequiresPermissions("product:brand:query")
    @Operation(summary = "id获取品牌详细信息")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@Parameter(description = "品牌id") @PathVariable("id") Long id){
        return success(brandService.getById(id)); //返回有结果的数据
    }

    @Log(title = "品牌管理", businessType = BusinessType.DELETE)
    @RequiresPermissions("product:brand:remove")
    @Operation(summary = "删除品牌")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(
            @Parameter(description = "品牌id列表")
            @PathVariable List<Long> ids) {
        return toAjax(brandService.removeBatchByIds(ids));
    }

    /**
     * 新增品牌
     */
    @RequiresPermissions("product:brand:add")
    @Log(title = "品牌管理", businessType = BusinessType.INSERT)
    @Operation(summary = "新增品牌")
    @PostMapping
    public AjaxResult add(/*@Validated */@RequestBody Brand brand)
    {
       /* if (!postService.checkPostNameUnique(post))
        {
            return error("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        }
        else if (!postService.checkPostCodeUnique(post))
        {
            return error("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }*/
        brand.setCreateBy(SecurityUtils.getUsername());
        return toAjax(brandService.save(brand));
    }

    /**
     * 修改品牌
     */
    @RequiresPermissions("product:brand:edit")
    @Log(title = "品牌管理", businessType = BusinessType.UPDATE)
    @Operation(summary = "修改品牌")
    @PutMapping
    public AjaxResult edit(/*@Validated*/ @RequestBody Brand brand)
    {
       /* if (!postService.checkPostNameUnique(post))
        {
            return error("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        }
        else if (!postService.checkPostCodeUnique(post))
        {
            return error("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }*/
        brand.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(brandService.updateById(brand));
    }

    @Log(title = "品牌管理", businessType = BusinessType.QUERY)
    //   @RequiresRoles("admin")    //角色
    @Operation(summary = "品牌列表")
    @GetMapping("/getBrandAll")
    public AjaxResult list()
    {
        List<Brand> list = brandService.list();
        return success(list);
    }



}
